﻿using Domain;
using Domain.Entity.Config;
using Domain.Repository.IConfig;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository.Config
{
    public class EIRepository : IEngage_interviewRepository
    {
        private readonly MyDbContext myDbContext;

        public EIRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public async Task<bool> AddEIAsync(Engage_interview ei)
        {
            ei.Check_status = 1;
            myDbContext.engage_Interviews.Add(ei);
            int rs=await myDbContext.SaveChangesAsync();
            return rs > 0;
        }

        public EIFenYe DateEIAsync(int page, int pageSize, int zt)
        {
            var query=myDbContext.engage_Interviews.AsQueryable();
            if (!string.IsNullOrEmpty(zt.ToString()))
            {
                query = query.Where(s => s.Check_status == zt);
            }
            int totalRecords=query.Count();
            var EI=query.Skip((page-1 )* pageSize  ).Take(pageSize).ToList();
            return new EIFenYe
            {
                totalRecords = totalRecords,
                ei = EI
            };
        }

        public async Task<bool> DeleteEIAsync(int id)
        {
            Engage_interview ei = new Engage_interview()
            {
                Ein_id = id
            };
            myDbContext.engage_Interviews.Remove(ei);
            int changes=await   myDbContext.SaveChangesAsync(); 
            return changes > 0;
        }

        public async Task<Engage_interview> GetEIByNameAsync(int id)
        {
            return await myDbContext.engage_Interviews.Where(e => e.Resume_id == id).FirstOrDefaultAsync();
        }

        public async Task<bool> UpdateEIAsync(Engage_interview ei)
        {
            Engage_interview eis=await myDbContext .engage_Interviews.Where(e=>e.Ein_id==ei.Ein_id).FirstOrDefaultAsync();
            eis.Checker=ei.Checker;
            eis.Check_time=ei.Check_time;
            eis.Check_comment=ei.Check_comment;
            myDbContext .engage_Interviews.Update(eis);
            return await myDbContext.SaveChangesAsync() > 0;
        }

        public async Task<bool> UpdateEISAsync(Engage_interview ei)
        {
            myDbContext .engage_Interviews.Update(ei);
            return await myDbContext.SaveChangesAsync() > 0;
        }
    }
}
